package com.viphome.dao.impl;

import com.viphome.bean.Student;
import com.viphome.dao.StudentDao;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangzhanjin on 2017/12/20.
 */
public class StudentDaoSpringJdbcImpl  implements StudentDao {
    private JdbcTemplate jdbcTemplate;


    public List<Student> query() {

       final  List<Student> students = new ArrayList<Student>();
        String sql = "SELECT id,name,age FROM studen";
        //RowCallbackHandler
        jdbcTemplate.query(sql, new RowCallbackHandler() {
            public void processRow(ResultSet resultSet) throws SQLException {
                while (resultSet.next()){
                    int id = resultSet.getInt("id");
                    String name = resultSet.getString("name");
                    int age = resultSet.getInt("age");
                    Student student = new Student();
                    student.setId(id);
                    student.setName(name);
                    student.setAge(age);

                    //这里会报错，需要添加一个最终类
                    students.add(student);
                }
            }
        });

        return students;
    }

    public void save(Student student) {

    }
}
